﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="../style.css">
<link href="../tablecloth/tablecloth.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript" src="../tablecloth/tablecloth.js"></script>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body>

<?php	

	include("../inc/joomlaInt_admin.php");
	include("../inc/inc_configuration.php");

if ($jUser==0)
{
	echo "Bitte zuerst einloggen!";
}
else
{	
	
	$q="SELECT gid FROM $tbl_users WHERE id=$jUser";
					$qe=mysql_query($q);
					$qeo=mysql_fetch_object($qe);
					
					$gid=$qeo->gid;
					
					if ($gid!=$admingroup)
					{
						echo "Du bist kein Admin!";
						break;
					}
					
	$liga=$_GET["liga"];
	$matchday=$_GET["matchday"];
	
	if ($matchday=="")
	{
		$matchday=1;
	}
	
	$lq = "SELECT * FROM $tbl_lt WHERE leagueId=$liga";
	$lqe = mysql_query($lq);
	$lqc = mysql_num_rows($lqe);

	$mAnz = ($lqc-1)*2;

	$count=1;

	for($count = 1; $count < $mAnz+1; $count++)
	{
		$class="";
		if ($count==$matchday)
		{
			$class="class='selected'";
		}
		echo "<a $class href='ligaspiele.php?matchday=$count&liga=$liga'>$count</a> ";
	}
	
	echo "<table>
			<tr>
				<th>Id</th>
				<th>HomeTeam</th>
				<th>User</th>
				<th>H</th>
				<th>:</th>
				<th>G</th>
				<th>User</th>
				<th>GastTeam</th>
				<th>frei?</th>
				<th>date</th>
				<th></th>
				<th></th>
			</tr>";
	
	$mq=	"SELECT 		m1.id AS mId, 
							m1.homeG AS hG,
							m1.guestG AS gG,
							m1.playdate AS pd,
							m1.matchday AS spieltag, 
							m1.homeT AS hTi, 
							m1.guestT AS gTi, 
							t1.name AS hTn, 
							t2.name AS gTn,
							m1.homeC AS hC,
							m1.guestC AS gC,
							m1.free AS free
							
						FROM $tbl_matches m1, $tbl_teams t1, $tbl_teams t2
						
						WHERE
						m1.leagueId=$liga AND
						m1.homeT=t1.id AND
						m1.guestT=t2.id AND
						m1.matchday=$matchday
					";
					
	$mqe=mysql_query($mq);
				
	while ($row=mysql_fetch_object($mqe))
	{
		$mId=$row->mId;
		$tHi=$row->hTi;
		$tGi=$row->gTi;
		$tHn=$row->hTn;
		$tGn=$row->gTn;
		$hG=$row->hG;
		$gG=$row->gG;
		$playdate=$row->pd;
		$spieltag=$row->spieltag;
		
		$free=$row->free;
		
		$playdate=date("d.m.y - H:i",$playdate);
		
		if ($free>=3)
		{
			$cHi=$row->hC;
			$cGi=$row->gC;
			
			$uHSql="	SELECT username FROM $tbl_combinations c, $tbl_users u
							WHERE
							(c.id=$cHi) AND
							(c.userId=u.id)";
			$uGSql="	SELECT username FROM $tbl_combinations c, $tbl_users u
							WHERE
							(c.id=$cGi) AND
							(c.userId=u.id)";
							
			$uHq = mysql_query($uHSql);
			$uGq = mysql_query($uGSql);
			
			$uHnq = mysql_fetch_object($uHq);
			$uHn = $uHnq->username;
			
			$uGnq = mysql_fetch_object($uGq);
			$uGn = $uGnq->username;
			
		}
		else
		{
			//Combinations suchen
			$uHSql="	SELECT username AS uHn, c.id AS hC FROM $tbl_combinations c, $tbl_users u
							WHERE
							(c.teamId=$tHi) AND
							(c.active=1) AND
							(c.userId=u.id)";
			$uGSql="	SELECT username AS uGn, c.id AS gC FROM $tbl_combinations c, $tbl_users u
					WHERE
					(c.teamId=$tGi) AND
					(c.active=1) AND
					(c.userId=u.id)";
					
			$uHq = mysql_query($uHSql);
			$uGq = mysql_query($uGSql);

			
			$cH = mySQL_num_rows($uHq);
			$cG = mySQL_num_rows($uGq);
			
			if ($cH == 1)
			{
				$uHnq = mysql_fetch_object($uHq);
				$uHn = $uHnq->uHn;
				
			}
			else
			{
				$uHn = "(-)";
			}
			
			if ($cG == 1)
			{
				$uGnq = mysql_fetch_object($uGq);
				$uGn = $uGnq->uGn;
				
			}
			else
			{
				$uGn = "(-)";
			}
		}
		
		
		
		if ($hG=="" OR $hG=="NULL")
		{
			$hG="-";
		}
		if ($gG=="" OR $gG=="NULL")
		{
			$gG="-";
		}
		
		if ($playdate=="")
		{
			$playdate="-";
		}
		
		
		echo "	<tr>
					<td>$mId</td>
					<td>$tHn</td>
					<td>$uHn</td>
					<td>$hG</td>
					<td>:</td>
					<td>$gG</td>
					<td>$uGn</td>
					<td>$tGn</td>
					<td>$free</td>
					<td>$playdate</td>
					<td><a href='spiel_entfernen.php?matchId=",$mId,"' class='minibutton entfernen'></a></td>
					<td><a href='spiel_edit.php?match=",$mId,"' class='minibutton bearbeiten'></a></td>
				</tr>";	
	}
	
	echo "<tr><th colspan='12'></th></tr></table>";
	
	echo "<br><br><center><a href='spiel_anlegen.php?liga=$liga' class='button anlegen'>Spiel anlegen</a>";
	
	echo "<br><br><center><a href='liga.php?liga=$liga' class='button zurueck'>Zurück</a>";
}	
?>

</body>
</html>